<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>新品有戏后台管理</title>
    <link href="__CSS__/boot.css" rel="stylesheet">
    <link href="__STATIC__/layui/css/layui.css" rel="stylesheet">
    <script src="__JS__/jquery.js"></script>
    <script src="__JS__/layer/layer.js"></script>
    <script src="__STATIC__/layui/layui.js"></script>
    <style>
        th{
            text-align: center;
            vertical-align: middle!important;
        }
    </style>
</head>
<body>
{include file="base/header" /}

<div style="width: 500px;margin: 0 auto">
    <form class="layui-form" action="{:url('index/order/doPurchase')}" method="post">
        <input type="hidden" name="bid" value="{$bid}"/>
        <div class="layui-form-item">
            <label class="layui-form-label"  style="width: 100px">地址</label>
            <div class="layui-input-block">
                <select name="city" lay-verify="required">
                    <option value="请选择">请选择</option>
                    {foreach $shop as $val}
                        <option value="{$val.address}">{$val.address}</option>
                    {/foreach}
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"  style="width: 100px">付款方式</label>
            <div class="layui-input-block">
                <select name="payment" lay-verify="required" disabled>
                    {if $successively == 1}
                        <option value="{$successively}"> 先付款 </option>
                    {else/}
                        <option value="{$successively}"> 后付款 </option>
                    {/if}
                </select>
            </div>
        </div>
        {foreach $result as $val}
            <div class="layui-form-item">
                <input type="hidden" name="id[]" value="{$val.id}">
                <label class="layui-form-label"  style="width: 100px">{$val.pname}</label>
                <div class="layui-input-block">
                    <input type="text" name="number[]" required  lay-verify="required" placeholder="采购数量（个）" autocomplete="off" class="layui-input">
                </div>
            </div>
        {/foreach}
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label"  style="width: 100px">描述</label>
            <div class="layui-input-block">
                <textarea name="describe" id="describe" placeholder="请输入内容" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="button" onclick="sub()" class="btn btn-primary">立即采购</button>
            </div>
        </div>
    </form>
    <script>
        //Demo
        layui.use('form', function(){
            //var form = layui.form;

            //监听提交
//            form.on('submit(formDemo)', function(data){
//                layer.msg(JSON.stringify(data.field));
//                return false;
//            });
        });
    </script>
    <script>

        function sub() {
            pid = [];
            $('input[name="id[]"]').each(function(){
                pid.push($(this).val());
            });
            num = [];
            $('input[name="number[]"]').each(function(){
                num.push($(this).val());
            });
            var city = $("select[name=city]").val();
            var bid = $("input[name=bid]").val();
            var payment = $("select[name=payment]").val();
            var describe = $("#describe").val();
            if(city == '请选择'){
                layer.alert('请选择地址');
                return;
            }
            if(payment == '请选择'){
                layer.alert('请选择支付方式');
                return;
            }

            if(!num){
                layer.alert('请填写数量');
                return;
            }
            $.ajax({
                url:"{:url('index/order/doPurchase')}",
                type:"post",
                data:"city="+city+"&num="+num+'&pid='+pid+"&bid="+bid+"&payment="+payment+"&describe="+describe,
                dataType:"json",
                success:function (data) {
                    if(data.success){
                        layer.alert(data.success);
                    }
                    if(data.error){
                        layer.alert(data.error);
                    }
                }
            });
        }
    </script>
</div>

</body>
</html>